<?php
class CommonAction extends Action
{
	public function _initialize() {
		$userAuthKey = session(C('USER_AUTH_KEY'));
		if ( !isset($userAuthKey) || !$userAuthKey )
			redirect ( PHP_FILE . C ( 'USER_AUTH_GATEWAY' ) );
	}
	
	public function getLoginId() {
		return session(C('USER_AUTH_KEY')) ? session(C('USER_AUTH_KEY')) : '';
	}
	
	public function getAccount() {
		return session('account') ? session('account') : '';
	}
	/**
	 * ===========
	 * 公共函数
	 * 若有特殊需求，派生类中重写该方法
	 * @param HashMap $map 过滤条件
	 * ===========
	 */
	public function index() {
		$actionName = $this -> getActionName();
		
		$Model = D($actionName);
		if (! empty($Model) ) {
			$this -> _list( $Model );		
		}
		
		$this -> display();
		return;
	}
	
	public function getReturnUrl() {
		return __URL__ . '?' . C( 'VAR_MODULE' ) .'=' . MODULE_NAME . '&' . C( 'VAR_ACTION' ) . '=' . C( 'DEFAULT_ACTION' );		
	}
	
	/**
	 * @param array $map 过滤条件
	 * @param object $Model
	 */
	protected function _list( $Model, $map=array(), $ext=false, $order=array() ) {
		$count = $Model -> where($map) -> count ( 'id' );
		
		if ($count > 0) {
			import ( "@.ORG.Page" );
			//创建分页对象 listRows 是表单传过来的每页显示记录数
			$listRows = '';
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			}
			
			$p = new Page ( $count, $listRows );
			$voList = $Model -> where($map) -> limit($p->firstRow . ',' . $p->listRows) -> order($order) -> select();
			
			if ( $ext )
				$voList = $this -> outputExt($voList);
			
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list', $voList);
		}
		
		$this->assign ( 'totalCount', $count );
		$this->assign ( 'numPerPage', C('PAGE_LISTROWS') );
		$this->assign ( 'currentPage', ($this->_param(C('VAR_PAGE')))?$this->_param(C('VAR_PAGE')):1);
		
		cookie('_currentUrl_', __SELF__);
		return;
	}
	
	/**
	 * 删除记录
	 */
	public function delete( $Model='' ) {
		$id = $this -> _get('id');
		if (!$id)
			$this -> error('参数错误！');
		
		if ( empty($Model) ) {
			$actionName = $this -> getActionName();
			$Model = D($actionName);
		}
		
		if(empty($Model))
			$this -> error('模型对象不存在！');
		
		$sql = 'id='.$id;
		$ret = $Model -> where($sql) -> delete();
		if (!$ret)
			$this -> error($Model -> getError());
		else {
			D('Log') -> actionLog(LogModel::DEL, '删除'.$actionName.'相关记录！记录id：'.$id);
			$this -> success('删除成功！');
		}
			
		
	}
	
	/**
	 * 添加一条新记录
	 */
	public function insert() {
		$actionName = $this -> getActionName();
		$Model = D($actionName);
		if (false === $Model -> create ()) {
			$this->error ( $Model -> getError () );
		}

		$this -> insertExt($Model); 
		$this -> insertCheck($Model);
		
		$ret = $Model -> add();
		if (!$ret) 
			$this -> error('添加记录失败！');
		else {
			D('Log') -> actionLog(LogModel::ADD, '添加'.$actionName.'相关记录！');
			$this -> success('添加记录成功！');
		}
	}
	
	/**
	 * 获取所有文章类别
	 */
	protected function getArticleCategorys( $field='*' ) {
		$Model = D('ArticleCategory');
		
		$record = $Model -> field($field)
			-> where('status = 1')
			->select();
		
		$categoryList = array();
		if ($record) {
			foreach ($record as $key => $val) {
				$categoryList[$val['id']] = $val;
			}
		}
		
		return $categoryList;
	}
	
	/**
	 * 插入字段检查，派生类中实现
	 * @param object $Model
	 */
	protected function insertCheck($Model) {}
	/**
	 * 扩展字段，输入
	 * @param object $Model
	 */
	protected function insertExt($Model) {}
	
	/**
	 * 扩展字段，输出
	 */
	protected function outputExt($list) {
		return $list;
	}
	
	/**
	 * 抛出错误提示
	 * @param string $msg
	 */
	public function alertError( $msg ) {
		echo '<script>alertMsg.error("'.$msg.'");</script>';
		exit;
	}
	
	/**
	 * 抛出警告信息
	 * @param string $msg
	 */
	public function alertWarn( $msg ) {
		echo '<script>alertMsg.warn("'.$msg.'");</script>';
		exit;
	}
	
}
